package com.paic.custserv.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.stereotype.Service;

import com.paic.custserv.domain.CussResource;
import com.paic.custserv.mapper.CussResourceMapper;
import com.paic.custserv.service.ICussRoleAuthorityService;
import com.paic.custserv.utils.BeanUtils;
import com.paic.custserv.utils.MenuConverUtil;
import com.paic.custserv.vo.SysMenuVO;

@Service("cussRoleAuthorityService")
public class CussRoleAuthorityServiceImpl implements ICussRoleAuthorityService {
	
	@Autowired
	private CussResourceMapper cussResourceMapper;

	@Override
	public List<SysMenuVO> fillMenuInfo(String roleId) {
		List<CussResource> list = cussResourceMapper.selectByRoleId(roleId);
		List<SysMenuVO> sysMenuList = BeanUtils.copyAs(list, SysMenuVO.class);
		return MenuConverUtil.fillMenuInfo(sysMenuList);
	}

	@Override
	public Map<String, Collection<ConfigAttribute>> loadAllResourceMenu() {
		List<CussResource> list = cussResourceMapper.selectAll();
		Map<String, Collection<ConfigAttribute>> resourceMap = new HashMap<String, Collection<ConfigAttribute>>();
        for(CussResource resource : list) {
        	 Collection<ConfigAttribute> atts = resourceMap.get(resource.getResourceUrl());
        	 if(atts != null) {
        		 atts.add(new SecurityConfig(String.valueOf(resource.getIdResource())));
        	 }else {
        		 Collection<ConfigAttribute> newAtts = new ArrayList<ConfigAttribute>();
        		 ConfigAttribute ca = new SecurityConfig(String.valueOf(resource.getIdResource()));
        		 newAtts.add(ca);
        		 resourceMap.put(resource.getResourceUrl(), newAtts);
        	 }
        }
		
        return resourceMap;
	}
	
	

}
